// coach has been here!
#include <cstdio>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <string.h>
using namespace std;

#define foreach( it, a ) for ( __typeof((a).begin()) it = (a).begin(); it != (a).end(); it++ )
#define forn( i, n ) for ( int i=0; i<(int)(n); i++ )

set<string> s1, s2;
map<string,int> mm;
char s[9870];

int max( int a, size_t b ) {
  return a < b ? b : a;
}

int main() {
  FILE* f = fopen( "..\\Data\\training.csv", "r" );

  int ind1 = 5;
  int ind2 = 16;

  fgets( s, 8000, f );

    vector<string> vv;
    int n = strlen( s );
    s[n++] = ',', s[n] = 0;
    string cur = "";
    for ( int i = 0; i < n; i++ )
      if ( s[i] == ',' ) {
        vv.push_back( cur );
        cur = "";
      }
      else cur += s[i];

  fclose( f );

  forn( i, vv.size() ) printf( "%d : %s\n", i, vv[i].c_str() );
  printf( "Enter two indices: " );
  scanf( "%d %d", &ind1, &ind2 );
  printf( "Reading..." );

  freopen( "..\\Data\\training.csv", "r", stdin );
  freopen( "training.rsv", "w", stdout );

  gets( s );
  while ( gets( s ) ) {
    vv.clear();
    int n = strlen( s );
    s[n++] = ',', s[n] = 0;
    string cur = "";
    for ( int i = 0; i < n; i++ )
      if ( s[i] == ',' ) {
        vv.push_back( cur );
        cur = "";
      }
      else cur += s[i];
    s1.insert( vv[ind1] );
    s2.insert( vv[ind2] );
    mm[ vv[ind1] + "*" + vv[ind2] ]++;
  }

//  size_t mxCol = 0;
//  foreach( it, s2 ) if ( it->size() > mxCol ) mxCol = it->size();
  size_t mxRow = 0;
  foreach( it, s1 ) if ( it->size() > mxRow ) mxRow = it->size();

  forn( i, mxRow ) printf( " " );
  printf( " |" );
  foreach( it, s2 ) printf( " %*s |", 7, it->c_str() );
  printf( "\n" );
  foreach( it1, s1 ) {
    printf( "%*s |", mxRow, it1->c_str() );
    double sum = 0;
    foreach( it2, s2 ) sum += mm[(*it1) + "*" + (*it2)];
    foreach( it2, s2 ) 
      if ( mm[(*it1) + "*" + (*it2)] == 0 ) printf( " %*s |", max( 7, it2->size() ), "n/a" );
      else printf( " %*.4f |", max( 7, it2->size() ), mm[(*it1) + "*" + (*it2)] / sum * 99.9999 );
    printf( " total = %d", (int)(sum + 0.1) );
    printf( "\n" );
  }

  fprintf( stderr, "done!\n" );

  return 0;
}